package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.DownloadUtil;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencEdu2ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.SaveEdu2Bean;
import patrol.mobile.com.mobilepatrol.bean.SaveEdu2TopBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.HttpDownloader;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencySaveEdu2Activity extends CommonListActivity {

    private TextView tv_educateName;
    private TextView tv_createDate;
    private TextView tv_educateNumber;
    private TextView tv_partakeUnit;
    private TextView tv_partakeName;
    private TextView tv_org;

    private EditText et_content;
    private EditText et_summary;

    private View itemViewYop;
    private List<SaveEdu2Bean> ensureMade2BeanList = new ArrayList<>();

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencEdu2ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_SAVE_EDU_2;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();

        itemViewYop = LayoutInflater.from(this).inflate(R.layout.activity_emergency_save_edu2, null);

        tv_educateName = itemViewYop.findViewById(R.id.tv_educateName);
        tv_createDate = itemViewYop.findViewById(R.id.tv_createDate);
        tv_educateNumber = itemViewYop.findViewById(R.id.tv_educateNumber);
        tv_partakeUnit = itemViewYop.findViewById(R.id.tv_partakeUnit);
        tv_partakeName = itemViewYop.findViewById(R.id.tv_partakeName);
        tv_org = itemViewYop.findViewById(R.id.tv_org);

        et_content = itemViewYop.findViewById(R.id.et_content);
        et_summary = itemViewYop.findViewById(R.id.et_summary);

        mAdapter.addHeaderView(itemViewYop);

        requestData(getIntent().getStringExtra("id"));


        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
position=position-1;
                final SaveEdu2Bean ensureMade2Bean = ensureMade2BeanList.get(position);
                final String url = baseUrl.substring(0, baseUrl.length() - 1) + ensureMade2Bean.getFilePath().replaceAll("\\\\", "/");
                final String path = "ensureMade";
                final String fileName = ensureMade2Bean.getFileName();
                final HttpDownloader httpDownloader = new HttpDownloader();

                DownloadUtil.get().download(url, Environment.getExternalStorageDirectory().getAbsolutePath(), fileName, new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        try {
                            File file2 = new File(file.getAbsolutePath());
                            Intent intent2 = new Intent("android.intent.action.VIEW");
                            intent2.addCategory("android.intent.category.DEFAULT");
                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            Uri uri = Uri.fromFile(file2);
                            //application/msword
                            //application/vnd.ms-excel
                            if (ensureMade2Bean.getFileType().contains("doc")) {
                                intent2.setDataAndType(uri, "application/msword");
                            } else if (ensureMade2Bean.getFileType().contains("xlsx")) {
                                intent2.setDataAndType(uri, "application/vnd.ms-excel");
                            } else {
                                intent2.setDataAndType(uri, "text/plain");
                            }
                            startActivity(intent2);
                        } catch (Exception e) {
                            //没有安装第三方的软件会提示
                            ToastUtils.showToast("没有找到打开该文件的应用程序");
                        }
                    }

                    @Override
                    public void onDownloading(int progress) {

                    }

                    @Override
                    public void onDownloadFailed(Exception e) {

                    }
                });
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        httpDownloader.downlaodFile(url, path, fileName);
//
//                    }
//                }).start();
//                httpDownloader.setDownListener(new HttpDownloader.IDownListener() {
//                    @Override
//                    public void result(int result) {
//                        if (result == -1) {
//                            ToastUtils.showToast("下载失败，无法打开");
//                            return ;
//                        }
//                        try {
//                            File file = new File(FileUtil.getSDPATH() + path + fileName);
//                            Intent intent2 = new Intent("android.intent.action.VIEW");
//                            intent2.addCategory("android.intent.category.DEFAULT");
//                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                            Uri uri = Uri.fromFile(file);
//                            //application/msword
//                            //application/vnd.ms-excel
//                            if (ensureMade2Bean.getFileType().contains(".docx")) {
//                                intent2.setDataAndType(uri, "application/msword");
//                            } else if (ensureMade2Bean.getFileType().contains(".xlsx")) {
//                                intent2.setDataAndType(uri, "application/vnd.ms-excel");
//                            } else {
//                                intent2.setDataAndType(uri, "text/plain");
//                            }
//                            startActivity(intent2);
//                        } catch (Exception e) {
//                            //没有安装第三方的软件会提示
//                            ToastUtils.showToast("没有找到打开该文件的应用程序");
//                        }
//                    }
//                });
            }
        });

    }

    private void requestData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_EDU_2);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {


            }

            @Override
            public void onResponse(String code, String response, String attachList) {
                SaveEdu2TopBean saveEdu2TopBean = GsonUtil.GsonToBean(response, SaveEdu2TopBean.class);
                tv_educateName.setText(saveEdu2TopBean.getEducateName());
                tv_createDate.setText(saveEdu2TopBean.getCreateDate());
                tv_educateNumber.setText(saveEdu2TopBean.getEducateNumber());
                tv_partakeUnit.setText(saveEdu2TopBean.getPartakeUnit());
                tv_partakeName.setText(saveEdu2TopBean.getPartakeName());
                tv_org.setText(saveEdu2TopBean.getMainOrgName());
                et_content.setText(saveEdu2TopBean.getContent());
                et_summary.setText(saveEdu2TopBean.getSummary());


                if (!TextUtils.isEmpty(attachList)) {
                    ensureMade2BeanList = GsonUtil.jsonToList(attachList, SaveEdu2Bean.class);
                    mAdapter.updateItems(ensureMade2BeanList);
                }
            }
        });

    }


}
